import logging
import os
import shutil
from datetime import datetime, timedelta
from pathlib import Path
import ddddocr
import pytest
from tools.page import page_get
from tools.发送钉钉消息 import send_dingding_message

logger = logging.getLogger(__name__)


@pytest.fixture(scope="session", autouse=True, name='date_hour')
def date_hour_():
    date_hour = datetime.now().strftime('%H-%M-%S')
    yield date_hour

@pytest.fixture(scope="session", autouse=True, name='timestamp')
def get_timestamp():
    return datetime.now().strftime('%Y%m%d%H%M')

# 用例执行失败时发送钉钉消息
def pytest_terminal_summary(terminalreporter):
    if terminalreporter.stats.get("failed"):
        send_dingding_message()

# 删除除今天和昨天外的其他数据
@pytest.fixture(scope="session", autouse=True)
def del_file(path='C:/退件新增订单'):
    for elm in Path(path).glob('*'):
        today = datetime.now().strftime('%Y-%m-%d')
        yesterday = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')
        if elm == Path(f'C:/退件新增订单/{today}') or elm == Path(f'C:/退件新增订单/{yesterday}'):
            continue
        elm.unlink() if elm.is_file() else shutil.rmtree(elm)
